package com.proback.service.impl;

import com.proback.entity.User;
import com.proback.repository.UserRepository;
import com.proback.service.UserService;
import com.proback.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User register(String account, String username, String password) throws Exception {
        Optional<User> existingUser = userRepository.findByAccount(account);
        if (existingUser.isPresent()) {
            throw new Exception("该手机号已注册");
        }

        User user = new User();
        user.setAccount(account);
        user.setUsername(username);
        user.setPassword(Md5Util.encrypt(password));
        user.setAddTime(LocalDateTime.now());
        user.setBookNum(0);

        return userRepository.save(user);
    }

    @Override
    public User login(String account, String password) throws Exception {
        Optional<User> userOpt = userRepository.findByAccount(account);
        if (userOpt.isEmpty()) {
            throw new Exception("用户不存在");
        }

        User user = userOpt.get();
        String encryptedPassword = Md5Util.encrypt(password);

        if (!user.getPassword().equals(encryptedPassword)) {
            throw new Exception("密码错误");
        }

        return user;
    }

    @Override
    public void deleteUserById(Integer id) throws Exception {
        Optional<User> userOpt = userRepository.findById(id);
        if (userOpt.isEmpty()) {
            throw new Exception("用户不存在");
        }

        userRepository.deleteById(id);
    }

    @Override
    public User getUserById(Integer id) {
        return userRepository.findById(id).orElse(null);
    }


    @Override
    public User updateUsername(Integer userId, String newUsername) throws Exception {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new Exception("用户不存在"));

        // 更新用户名
        user.setUsername(newUsername);

        // 保存到数据库
        return userRepository.save(user);
    }


    @Override
    public void changePassword(Integer userId, String oldPassword, String newPassword) throws Exception {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new Exception("用户不存在"));

        // 对前端传来的明文旧密码进行MD5加密
        String encryptedOldPassword = Md5Util.encrypt(oldPassword);

        // 与数据库中存储的MD5加密密码比较
        if (!encryptedOldPassword.equals(user.getPassword())) {
            throw new Exception("当前密码不正确");
        }

        // 验证新密码强度
        if (newPassword == null || newPassword.length() < 6) {
            throw new Exception("新密码长度至少为6位");
        }

        // 对新密码进行MD5加密
        String encryptedNewPassword = Md5Util.encrypt(newPassword);
        user.setPassword(encryptedNewPassword);

        userRepository.save(user);
    }
}
